//对象解构赋值
let obj={a:1,b:2};
//let {a,b,c}=obj;
//console.log('a:',a) //a: 1
//console.log('b:',b) //b: 2
//console.log('c:',c) //c: undefined

//变量转移,前面定义过let，后面不能再let定义了，得去除let直接赋值
//{a:A}=obj;  //注意花括号不能在外面，否则会被当做代码块
({a:A,b}=obj);  //要用括号包裹
console.log('A:',A); //A: 1
console.log('b:',b); //b: 2
//console.log('a:',a); //a is not defined

//默认值
let {d=1,e=2}={d:10};
console.log(d,e); //10  2
let {f:F=1,g=2}={f:10}; //变量替换
console.log(F,g); //10  2

//解构自有对象
let {floor,abs} = Math;
console.log(floor(12.7)); //12
console.log(abs(-12.7));  //12.7

//解构属性
let {length}='sadskadnk';
console.log('字符串长度：',length);  //9

//解构字符串
let [wo,ai,ni]='我爱你';
console.log(wo,ai,ni);  //我 爱 你

//解构参数
var arr=[1,2];
var obj1={m:3,n:4};
function test1([a,b]) {
	console.log(a,b);	
}
test1(arr);
function test2({m,n,i=10}) {
	console.log(m,n,i);	
}
test2(obj1);

/* 结构赋值用处 */
// 1. 变量初始化
/* ```
	let [a,b,c] = [1,2,3] 
	let {a,b,c} = {
		a:1,
		b:2,
		c:3
	}
``` */
// 2. 数据交互
/* ```
	Ajax(x,y,[a,b,c,{d,e,f}]=>{
		//...
	})
``` */
// 3. 数据交互时可以很方便的改键名
/* ```
	Ajax(x,y,res=>{
		res.forEach(item=>{
			let obj = {a,b,c:name} = item
			console.log(obj)   //{a:'',b:'',name:''}
		})
	})
``` */
// 4. 设置默认值
/* ```
	Ajax(x,y,res=>{
		res.forEach(item=>{
			//item = {a:'x',b:'x'} 没返回c,但自己写的测试数据中有c
			let obj = {a,b,c='暂无...'} = item
			console.log(obj)   //{a:'',b:'',c:'暂无'}
		})
	})
``` */
// 5. 交换两变量位置
/* ```
	let a = 5,b = 12
	[a,b] = [b,a]
	console.log(a,b)  //12,5
``` */
// 6. 调用构造函数或自己封装的函数时
/* ```
	function fn(){
		//...
		return {
			a:1,
			b:2
		}
	}
	let {a,b} = fn()
	console.log(a,b)
``` */

